﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Common;
    using MarketPulse.BusinessLogic.Common.Criteria;
    using MarketPulse.BusinessLogic.Core;
    using MarketPulse.BusinessLogic.Private.StateMachine;

#endregion

namespace MarketPulse.BusinessLogic.Private
{
    /// <summary>
    /// This class represents a Survey that is completed by a mystery shopper, auditor, phone operator or exit interviewer (for example). Assignments are created
    /// as part of a round and have an actual "site" or "branch" associated with them. Assignments also
    /// contain reference to the survey responses obtained when the Assignment and the data entry process has
    /// been completed, along with payment and charging information. Assignments have bot a Guid and versionId which, combined serve to uniquely 
    /// identifies the Assignment.
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.AssignmentFactory, MarketPulse.DataAccess.Factory")]
    public class Assignment : MarketPulseBase<Assignment>
    {

        #region Member Variables

            // TODO Refactor VisitStateContext to assignment

            /// <summary>
            /// The context object to use to manage the various states (and transitiosn) for an Assignment
            /// </summary>
            private IAssignmentStateContext mAssignmentStateContext;

            #region Association GUID Member Variables (Foreign Keys)

                /// <summary>
                /// The Guid of the Round associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mRoundGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.RoundGuid);

                /// <summary>
                /// The Guid of the User associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mUserGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.UserGuid);

                /// <summary>
                /// The Guid of the Branch associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mBranchGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.BranchGuid);

                /// <summary>
                /// The Guid of the Reponse associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mResponseGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.ResponseGuid);

                /// <summary>
                /// The Guid of the Survey associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mSurveyGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.SurveyGuid);

                /// <summary>
                /// The Version ID of the Survey associated with this Assignement. When used in conjunction with a SurveyID, this
                /// uniquely identifies a Survey
                /// </summary>
                public static PropertyInfo<Nullable<long>> mSurveyVersionIdProperty = RegisterProperty<Nullable<long>>(p => p.SurveyVersionId);

                /// <summary>
                /// The Guid of the AssignmentStatus associated with this Assignment 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> mAssignmentStatusTypeGuidProperty = RegisterProperty<Nullable<Guid>>(p => p.AssignmentStatusTypeGuid);

            #endregion

            #region DateTime Member Variables

                /// <summary>
                /// The start date and time after which the Assignment is valid to be carried out (separated into a specific date and a specific time)
                /// </summary>
                public static PropertyInfo<SmartDate> mValidStartDateTimeProperty = RegisterProperty<SmartDate>(p => p.ValidStartDateTime);

                /// <summary>
                /// The end date and time by which the Assignment must be carried out (separated into a specific date and a specific time)
                /// </summary>
                public static PropertyInfo<SmartDate> mValidEndDateTimeProperty = RegisterProperty<SmartDate>(p => p.ValidEndDateTime);

                /// <summary>
                /// The date and time that the Assignment is planned to be carried out
                /// </summary>
                public static PropertyInfo<SmartDate> mPlannedDateTimeProperty = RegisterProperty<SmartDate>(p => p.PlannedDateTime);

                /// <summary>
                /// The date and time that the Assignment was actually carried out (started)
                /// </summary>
                public static PropertyInfo<SmartDate> mActualStartDateTimeProperty = RegisterProperty<SmartDate>(p => p.ActualStartDateTime);

                /// <summary>
                /// The date and time that the Assignment was actually carried out (ended)
                /// </summary>
                public static PropertyInfo<SmartDate> mActualEndDateTimeProperty = RegisterProperty<SmartDate>(p => p.ActualEndDateTime);

            #endregion

            /// <summary>
            /// The label for this Assignment 
            /// </summary>
            public static PropertyInfo<string> mLabelProperty = RegisterProperty<string>(p => p.Label);

            /// <summary>
            /// The Version ID of this Assigment. When used in conjunction with the Guid, this
            /// uniquely identifies this Assignment
            /// </summary>
            public static PropertyInfo<Nullable<long>> mVersionIdProperty = RegisterProperty<Nullable<long>>(p => p.VersionId);

            /// <summary>
            /// The period of time after the actual Assignment is completed, within which data entry must be completed.
            /// </summary>
            public static PropertyInfo<TimeSpan> mDataEntryLagProperty = RegisterProperty<TimeSpan>(p => p.DataEntryLag);


            #region Association Member Variables (Related Objects)

                /// <summary>
                /// The Survey object associated with this Assignment (using SurveyGuid and SurveyVersionId)
                /// </summary>
                public static PropertyInfo<Survey> mSurveyProperty = RegisterProperty<Survey>(p => p.Survey);

                /// <summary>
                /// The Reponse object associated with this Assignment (using ResponseGuid)
                /// </summary>
                public static PropertyInfo<Response> mResponseProperty = RegisterProperty<Response>(p => p.Response);

                /// <summary>
                /// The BranchInfo object associated with this Assignment (using BranchGuid)
                /// </summary>
                private static PropertyInfo<BranchInfo> mBranchInfoProperty = RegisterProperty<BranchInfo>(p => p.Branch);

                /// <summary>
                /// The UserInfo object associated with this Assignment (using UserGuid)
                /// </summary>
                private static PropertyInfo<UserInfo> mUserInfoProperty = RegisterProperty<UserInfo>(p => p.User);

                /// <summary>
                /// The collection of AssignmentEvents that the Assignment has experienced during it's lifetime
                /// </summary>
                public static PropertyInfo<AssignmentEventList> mAssignmentEventListProperty = RegisterProperty<AssignmentEventList>(p => p.Events);

                /// <summary>
                /// The collection of AssignmentPayments that have been associated with the Assignment during it's lifetime
                /// </summary>
                public static PropertyInfo<AssignmentPaymentList> mAssignmentPaymentListProperty = RegisterProperty<AssignmentPaymentList>(p => p.Payments);

            #endregion

            /// <summary>
            /// Method used to release the Visit and make it ready to be assigned
            /// </summary>
            private static MethodInfo mReleaseMethod = RegisterMethod(typeof(Assignment), "Release");

            /// <summary>
            /// Method used to attach the Assignment to a specific user based on the userGuid value supplied
            /// </summary>
            private static MethodInfo mAssignMethod = RegisterMethod(typeof(Assignment), "Assign");

            /// <summary>
            /// Method used to save the information for this Assignment captured as part of the data entry process
            /// </summary>
            private static MethodInfo mStartDataEntryMethod = RegisterMethod(typeof(Assignment), "StartDataEntry");

            /// <summary>
            /// Method used to save the information for this Assignment captured as part of the data entry process
            /// </summary>
            private static MethodInfo mDataEnterMethod = RegisterMethod(typeof(Assignment), "DataEnter");

            /// <summary>
            /// Method used to complete the data entry process for this Assignment
            /// </summary>
            private static MethodInfo mCompleteDataEntryMethod = RegisterMethod(typeof(Assignment), "CompleteDataEntry");

        #endregion

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private Assignment()
            {
                mAssignmentStateContext = new AssignmentStateContext(AssignmentStatusTypeList.Created, this, true);
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method used to create a new Assignment object (via the DataPortal client proxy)
                /// </summary>
                /// <returns>The newly created Assignment object</returns>
                public static Assignment New()
                {
                    return DataPortal.Create<Assignment>();
                }

                /// <summary>
                /// Method used to retrieve an existing Assignment object from the database using the provided Guid (via the DataPortal client proxy)
                /// </summary>
                /// <param name="guid">The Guid of the Assignment to retrieve</param>
                /// <param name="versionId">The VersionId of the Assignment to retrieve</param>
                /// <returns>A valid (populated) Assignment object from the database</returns>
                public static Assignment GetByGuidAndVersionId(Guid guid, long versionId)
                {
                    Assignment assignment = DataPortal.Fetch<Assignment>(new UniqueAssignmentCriteria(guid, versionId));
                    //visit.mVisitStateContext.SetState(VisitStateFactory.Create(visit.GetProperty(Assignment.VisitStatusTypeGUIDProperty).Value, true));
                    return assignment;
                }

                /// <summary>
                /// Method used to update the supplied Assignment object (via the DataPortal client proxy)
                /// </summary>
                /// <param name="assignment">The Assignment object to update</param>
                /// <returns>The updated Assignment object</returns>
                public static Assignment Update(Assignment assignment)
                {
                    return DataPortal.Update<Assignment>(assignment);
                }


            #endregion

            #region System Methods

                /// <summary>
                /// This method interrogates the internal state machine to ascertain
                /// if the event in question causes a valid state transition.
                /// </summary>
                /// <param name="stateName">The name of the state to transition to</param>
                /// <returns>A boolean representing whether the Assignment can transition to the state provided</returns>
                public bool CanTransition(string stateName)
                {
                    return mAssignmentStateContext.CanTransition(stateName);
                }

            #endregion

            #region Business Process Methods

                /// <summary>
                /// Method used to release the Visit and make it ready to be assigned
                /// </summary>
                public void Release()
                {
                    CanExecuteMethod(mReleaseMethod, true);
                    mAssignmentStateContext.Release(false);
                    AddEvent(VisitEventTypeList.Release);
                }

                #region Assign Methods


                    /// <summary>
                    /// Method used to attach the Assignment to a specific user based on the userGuid value supplied
                    /// </summary>
                    /// <param name="userGuid">The Guid of the user to associate with this Assignment</param>
                    /// <param name="plannedDateTime">The date and time the Assignment is planned to take place</param>
                    public void Assign(Guid userGuid, DateTime plannedDateTime)
                    {
                        //CanExecuteMethod(AssignMethod, true);
                        //mVisitStateContext.Assign(false, userGuid, plannedDateTime);

                        UserGuid = userGuid;
                        PlannedDateTime = plannedDateTime;

                        //temp set status to assigned
                        AssignmentStatusTypeGuid = Guid.Parse("62FD3065-EB39-483D-81E0-50BC1DF84551");

                        AddEvent(VisitEventTypeList.Assign);
                    }

                #endregion

                #region DataEntryMethods

                    /// <summary>
                    /// Method used to save the information for this Assignment captured as part of the data entry process
                    /// </summary>
                    public void StartDataEntry()
                    {
                        CanExecuteMethod(mStartDataEntryMethod, true);
                        mAssignmentStateContext.StartDataEntry(false);
                        AddEvent(VisitEventTypeList.StartDataEntry);
                    }

                    /// <summary>
                    /// Method used to save the information for this Assignment captured as part of the data entry process
                    /// </summary>
                    /// <param name="responses">The XML response set to attach to the survey instance for the Assignment</param>
                    public void DataEnter(XElement responses)
                    {
                        CanExecuteMethod(mDataEnterMethod, true);
                        mAssignmentStateContext.DataEnter(false, responses);
                        SetResponses(responses);
                    }

                    /// <summary>
                    /// Method used to complete the data entry process for this Assignment
                    /// </summary>
                    public void CompleteDataEntry()
                    {
                        mAssignmentStateContext.CompleteDataEntry(false);
                        AddEvent(VisitEventTypeList.CompleteDataEntry);
                    }

                #endregion

            #endregion

            #region Local Methods (private)

                private void SetResponses(XElement responses)
                {
                    // Stamp the Assignment with the SurveyVersionId because data
                    // entry has now begun
                    SurveyVersionId = Survey.VersionID;

                    if (!FieldManager.FieldExists(mResponseProperty) && !ResponseGuid.HasValue)
                    {
                        Response response = Response.New();// (this);

                        response.ResponseXML = responses;
                        Response = response;
                    }
                    else if (ResponseGuid.HasValue)
                    {
                        Response.ResponseXML = responses;
                    }
                }

                /// <summary>
                /// Method used to add an AssignmentEvent based on the AssignmentEventTypeGuid supplied
                /// </summary>
                /// <param name="assignmentEventType">The Guid for the type of visit event to add</param>
                private void AddEvent(Guid assignmentEventType)
                {
                    AssignmentEvent assignmentEvent = AssignmentEvent.New();
                    assignmentEvent.AssignmentEventTypeGuid = assignmentEventType;
                    assignmentEvent.AssignmentGuid = this.GUID;

                    Events.Add(assignmentEvent);
                }

                /// <summary>
                /// Method used to update the value of a an AssignmentPayment with the new value supplied
                /// </summary>
                /// <param name="assignmentPaymentGuid">The Guid of the payment of update</param>
                private void UpdatePaymentAmount(Guid assignmentPaymentGuid, double amount)
                {
                    Payments.FindByGuid(assignmentPaymentGuid).Amount = amount;
                }

                /// <summary>
                /// Clones Assignment object.
                /// </summary>
                private void Clone()
                {
                    var newAssignment = Assignment.New();
                    newAssignment.AssignmentStatusTypeGuid = AssignmentStatusTypeGuid;
                    newAssignment.Branch = Branch;
                    newAssignment.BranchGuid = BranchGuid;
                    newAssignment.DataEntryLag = DataEntryLag;
                    newAssignment.Label = Label;
                    newAssignment.Response = Response;
                    newAssignment.ResponseGuid = ResponseGuid;
                    newAssignment.RoundGuid = RoundGuid;
                    newAssignment.SurveyGuid = SurveyGuid;
                    newAssignment.SurveyVersionId = SurveyVersionId;
                    newAssignment.User = User;
                    newAssignment.UserGuid = UserGuid;
                    newAssignment.VersionId = VersionId;
                    newAssignment.Save();
                }

            #endregion

        #endregion

        #region Properties

            #region Association GUID Properties (Foreign Keys)

                /// <summary>
                /// The Guid of the Round associated with this Assignment 
                /// </summary>
                public Nullable<Guid> RoundGuid 
                { 
                    get { return GetProperty(mRoundGuidProperty); }
                    private set { SetProperty(mRoundGuidProperty, value); }
                }

                /// <summary>
                /// The Guid of the User associated with this Assignment 
                /// </summary>
                public Nullable<Guid> UserGuid 
                { 
                    get { return GetProperty(mUserGuidProperty); }
                    private set { SetProperty(mUserGuidProperty, value); }
                }

                /// <summary>
                /// The Guid of the Branch associated with this Assignment 
                /// </summary>
                public Nullable<Guid> BranchGuid 
                { 
                    get { return GetProperty(mBranchGuidProperty); }
                    private set { SetProperty(mBranchGuidProperty, value); }
                }

                /// <summary>
                /// The Guid of the Response associated with this Assignment 
                /// </summary>
                public Nullable<Guid> ResponseGuid 
                { 
                    get { return GetProperty(mResponseGuidProperty); }
                    private set { SetProperty(mResponseGuidProperty, value); }
                }

                /// <summary>
                /// The Guid of the Survey associated with this Assignment 
                /// </summary>
                public Nullable<Guid> SurveyGuid 
                { 
                    get { return GetProperty(mSurveyGuidProperty); }
                    set { SetProperty(mSurveyGuidProperty, value); }
                }

                /// <summary>
                /// The Version ID of the Survey associated with this Assignement. When used in conjunction with a SurveyID, this
                /// uniquely identifies a Survey
                /// </summary>
                public Nullable<long> SurveyVersionId 
                { 
                    get { return GetProperty(mSurveyVersionIdProperty); }
                    set { SetProperty(mSurveyVersionIdProperty, value); }
                }

                /// <summary>
                /// The Guid of the AssignmentStatus associated with this Assignment 
                /// </summary>
                public Nullable<Guid> AssignmentStatusTypeGuid 
                { 
                    get { return GetProperty(mAssignmentStatusTypeGuidProperty); }
                    internal set { SetProperty(mAssignmentStatusTypeGuidProperty, value); }
                }

            #endregion

            #region DateTime Properties

                /// <summary>
                /// The start date and time after which the assignment is valid to be carried out (separated into a specific date and a specific time)
                /// </summary>
                public SmartDate ValidStartDateTime 
                { 
                    get { return GetProperty(mValidStartDateTimeProperty); }
                    set { SetProperty(mValidStartDateTimeProperty, value); }
                }

                /// <summary>
                /// The end date and time by which the assignment must be carried out (separated into a specific date and a specific time)
                /// </summary>
                public SmartDate ValidEndDateTime 
                { 
                    get { return GetProperty(mValidEndDateTimeProperty); }
                    set { SetProperty(mValidEndDateTimeProperty, value); }
                }

                /// <summary>
                /// The date and time that the Assignment is planned to be carried out
                /// </summary>
                public SmartDate PlannedDateTime 
                { 
                    get { return GetProperty(mPlannedDateTimeProperty); }
                    set { SetProperty(mPlannedDateTimeProperty, value); }
                }

                /// <summary>
                /// The date and time that the Assignment was actually carried out (started)
                /// </summary>
                public SmartDate ActualStartDateTime 
                { 
                    get { return GetProperty(mActualStartDateTimeProperty); }
                    set { SetProperty(mActualStartDateTimeProperty, value); }
                }

                /// <summary>
                /// The date and time that the Assignment was actually carried out (Ended)
                /// </summary>
                public SmartDate ActualEndDateTime 
                { 
                    get { return GetProperty(mActualEndDateTimeProperty); }
                    set { SetProperty(mActualEndDateTimeProperty, value); }
                }

            #endregion

            /// <summary>
            /// The Version ID of this Assigment. When used in conjunction with the Guid, this
            /// uniquely identifies this Assignment
            /// </summary>
            public Nullable<long> VersionId 
            { 
                get { return GetProperty(mVersionIdProperty); }
                set { SetProperty(mVersionIdProperty, value); }
            }

            ///// <summary>
            ///// The label for the Client associated with this Assignment 
            ///// </summary>
            //public string ClientLabel 
            //{ 
            //    get { return GetProperty(mClientLabelProperty); }
            //    set { SetProperty(mClientLabelProperty, value); }
            //}

            /// <summary>
            /// The label for his Assignment 
            /// </summary>
            public string Label 
            { 
                get { return GetProperty(mLabelProperty); }
                set { SetProperty(mLabelProperty, value); }
            }

            /// <summary>
            /// The period of time after the actual Assignment is completed, within which data entry must be completed.
            /// </summary>
            public TimeSpan DataEntryLag
            {
                get { return GetProperty(mDataEntryLagProperty); }
                set { SetProperty(mDataEntryLagProperty, value); }
            }

            #region Association Properties (Related Objects)

                /// <summary>
                /// The Survey object associated with this assignment (using SurveyGuid and SurveyVersionId)
                /// </summary>
                public Survey Survey
                {
                    get
                    {
                        // If we have an associated Guid value but the property hasn't been loaded yet
                        if (!FieldManager.FieldExists(mSurveyProperty) && SurveyGuid.HasValue)
                        {
                            // Check if there is a VersionId for the Survey, if not get the latest (no VersionId) otherwise get the specific Survey (Guid and VersionId)
                            if (!SurveyVersionId.HasValue)
                            {
                                return Survey.GetByGuid(SurveyGuid.Value);
                            }
                            else
                            {
                                return Survey.GetByGuidAndVersionId(SurveyGuid.Value, SurveyVersionId.Value);
                            }
                        }
                        else
                        {
                            return null;
                        }
                    }
                }

                /// <summary>
                /// The Reponse object associated with this Assignment (using ResponseGuid)
                /// </summary>
                public Response Response
                {
                    get
                    {
                        // If we have an associated Guid value but the property hasn't been loaded yet, load the property otherwise just get the current property
                        if (!FieldManager.FieldExists(mResponseProperty) && ResponseGuid.HasValue)
                        {
                            // Load the property and notify CSLA that the property has changed
                            LoadProperty(mResponseProperty, Response.GetByGuid(ResponseGuid.Value));
                            OnPropertyChanged(mResponseProperty);
                            return ReadProperty(mResponseProperty);
                        }
                        else if (FieldManager.FieldExists(mResponseProperty))
                        {
                            // Return the current property
                            SetProperty(mResponseGuidProperty, GetProperty(mResponseProperty).GUID);
                            return GetProperty(mResponseProperty);
                        }
                        else
                        {
                            return null;
                        }
                    }
                    set
                    {
                        // Set the associated Guid value and the association property (this) value
                        SetProperty(mResponseGuidProperty, value.GUID);
                        SetProperty(mResponseProperty, value);
                    }
                }

                /// <summary>
                /// The BranchInfo object associated with this Assignment (using BranchGuid)
                /// </summary>
                public BranchInfo Branch
                {
                    get
                    {
                        // If we have an associated GUID value but the property hasn't been loaded yet, load the property otherwise just get the current property
                        if (!FieldManager.FieldExists(mBranchInfoProperty) && BranchGuid.HasValue)
                        {
                            // Load the property and notify CSLA that the property has changed
                            LoadProperty(mBranchInfoProperty, BranchInfo.GetByGuid(BranchGuid.Value));
                            OnPropertyChanged(mBranchInfoProperty);
                            return ReadProperty(mBranchInfoProperty);
                        }
                        else if (FieldManager.FieldExists(mBranchInfoProperty))
                        {
                            // Return the current property
                            return GetProperty(mBranchInfoProperty);
                        }
                        else
                        {
                            return null;
                        }
                    }
                    set
                    {
                        // Set the associated GUID value and the association property (this) value
                        LoadProperty(mBranchGuidProperty, value.GUID);
                        LoadProperty(mBranchInfoProperty, value);
                    }
                }

                /// <summary>
                /// The UserInfo object associated with this Assignment (using UserGuid)
                /// </summary>
                public UserInfo User
                {
                    get
                    {
                        // If we have an associated Guid value but the property hasn't been loaded yet, load the property otherwise just get the current property
                        if (!FieldManager.FieldExists(mUserInfoProperty) && UserGuid.HasValue)
                        {
                            // Load the property and notify CSLA that the property has changed
                            LoadProperty(mUserInfoProperty, UserInfo.GetByGuid(UserGuid.Value));
                            OnPropertyChanged(mUserInfoProperty);
                        }
                        return ReadProperty(mUserInfoProperty);
                    }
                    set
                    {
                        // Set the associated GUID value and the association property (this) value
                        LoadProperty(mUserGuidProperty, value.GUID.Value);
                        LoadProperty(mUserInfoProperty, value);
                    }
                }

                /// <summary>
                /// The collection of AssignmentEvents that the Assignment has experienced during it's lifetime
                /// </summary>
                public AssignmentEventList Events
                {
                    get
                    {
                        // If the property hasn't been loaded yet
                        if (!FieldManager.FieldExists(mAssignmentEventListProperty))
                        {
                            if (IsNew)
                            {
                                // TODO check this new() method as it doesn't exist in the DataAccess!!!
                                // Load the property and notify CSLA that the property has changed
                                LoadProperty(mAssignmentEventListProperty, AssignmentEventList.New());
                                OnPropertyChanged(mAssignmentEventListProperty);
                            }
                            else
                            {
                                // Load the property
                                LoadProperty(mAssignmentEventListProperty, AssignmentEventList.GetByAssignmentGuid(this.GUID.Value));
                            }
                            return ReadProperty(mAssignmentEventListProperty);
                        }
                        else
                        {
                            // Return the current property
                            return GetProperty(mAssignmentEventListProperty);
                        }
                    }
                }

                /// <summary>
                /// The collection of AssignmentPayments that have been associated with the Assignment during it's lifetime
                /// </summary>
                public AssignmentPaymentList Payments
                {
                    get
                    {
                        // If the property hasn't been loaded yet
                        if (!FieldManager.FieldExists(mAssignmentPaymentListProperty))
                        {
                            if (IsNew)
                            {
                                // TODO check this new() method as it doesn't exist in the DataAccess!!!
                                // Load the property and notify CSLA that the property has changed
                                LoadProperty(mAssignmentPaymentListProperty, AssignmentPaymentList.New());
                                OnPropertyChanged(mAssignmentPaymentListProperty);
                            }
                            else
                            {
                                // Load the property
                                LoadProperty(mAssignmentPaymentListProperty, AssignmentPaymentList.GetByAssignmentGuidAndVersionId(this.GUID.Value, (long)this.VersionId));
                            }
                            return ReadProperty(mAssignmentPaymentListProperty);
                        }
                        else
                        {
                            // Return the current property
                            return GetProperty(mAssignmentPaymentListProperty);
                        }
                    }
                }

            #endregion

        #endregion
    }
}
